
cc.Class({
    extends: cc.Component,
    
    properties: {
        shootInterval:0.2,//射击子弹间隔
        yellowBullet:cc.Prefab,
        jigaungBullet:cc.Prefab,
        daodan:cc.Prefab,   
        HpProgress:cc.Node,
        shootNum1:10,
        shootNum2:10,
        shootNum3:20,
    },

    onLoad () {
        e = this;
        e.bossHp = DataMangrt.level * 70;
        e.init();
        // e.playAnimation();
        for(i=1;i<20;i++){
            this.delay +=  this.shoot(3);
            this.delay +=  this.shoot(1);
            this.delay +=  this.shoot(2);
            this.delay +=  this.shoot(1);
            this.delay +=  this.shoot(2);
        }
    },
    init:function(){
        this.isDie=false;
        this.anim = this.getComponent(cc.Animation);
        this.yellowBullet_pos = [-70,70,110];//子弹位置偏移量
        this.jigaungBullet_pos = [-120,120,80];
        this.daodan_pos = [-250,0,250,-200];
        this.HpProgress.getComponent(cc.ProgressBar).progress = 1;
        this.progressHp = this.bossHp;
        this.delay = 0;
        console.log(this.bossHp);
    },
    playAnimation:function () {
        e=this;
        // e.anim.play("enemyBossIdle");
    },
    shoot:function (shootType) {
        e=this;
        let shootTime = e.shootNum1 * e.shootInterval;
        switch (shootType) {
            case 1:
                let shootTime1 = this.delay;
                e.schedule(()=>{
                    e.creatBullet("bossBullet1",this.yellowBullet,this.yellowBullet_pos);//创建黄色子弹
                    e.creatBullet("bossBullet2",this.jigaungBullet,this.jigaungBullet_pos);//创建激光子弹
                },e.shootInterval,e.shootNum1, shootTime1 );//最后一个参数是延迟时间 
                return shootTime + 4;
                break;
            case 2:
                let shootTime2 = this.delay;
                e.schedule(()=>{
                    e.creatBullet("bossBullet3",this.daodan,this.daodan_pos);//创建导弹
                },e.shootInterval*1.5,e.shootNum2,shootTime2 );//最后一个参数是延迟时间 
                return shootTime * 1.5 + 4;
                break;
            case 3:
                let shootTime3 = this.delay;
                e.schedule(()=>{
                    e.creatBullet("bossBullet2",this.jigaungBullet,this.jigaungBullet_pos);//创建激光子弹
                },e.shootInterval*2,e.shootNum3,shootTime3 );//最后一个参数是延迟时间 
                return shootTime * 2 + 4;
                break;
            default:
                break;
        }
       

    },
    hit:function(bulletHarm){
       if (this.isDie ==false) {
        this.bossHp--;
        this.HpProgress.getComponent(cc.ProgressBar).progress -= 1/this.progressHp;
       }
       else{
        this.HpProgress.active = false;
       }
        // console.log('boss受伤,血量',this.bossHp);
    },
    creatBullet:function (poolName,prefab,pos) {
        if (DataMangrt.Game_isWin) return;
        let boss_pos = this.node.getPosition();
        let bulletNum = 0;
        let arrLen = pos;
        if (poolName == "bossBullet3") {
            bulletNum = 3;
        }
        else{
            bulletNum = 2;
        }
        for (let i = 0; i < bulletNum; i++) {
            let bulletNode = game.pools.getNode(poolName,prefab);
            bulletNode.parent = this.node.parent;
            bulletNode.setPosition(boss_pos.x - pos[i] ,boss_pos.y - pos[pos.length-1]);
        }
    },
   
    update (dt) {
        if (this.bossHp <= 0 && this.isDie == false) {
            this.isDie = true;
            // this.anim.stop();
            this.anim.play("enemyBoss");
            this.anim.on('finished', (event) => {
                console.log('Animation finished!');
                this.node.destroy();
                game.gameWin();//游戏胜利
            });
        }

    },
});
